﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;
using System.Data; 
namespace Solution.UI.Web.Controllers.Admin
{
    public class AdminTransactionController : Controller
    {
        private Business.Entities.SolutionEntities db = new SolutionEntities();
        //
        // GET: /AdminTransaction/

        public ActionResult Index()
        {
            //var list = Business.Component.TransactionComponent.Transaction_GetByTop("1000", "", "");
            //return View(list);
            var list = db.Transactions.OrderByDescending(c => c.TrId);
            return View(list.ToList());
        }

        public ActionResult Details(int id = 0)
        {
            Transaction transaction = db.Transactions.Find(id);
            if (transaction == null)
            {
                return HttpNotFound();
            }
            return View(transaction);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Transaction transaction, int TrGroup, int TrGroup2)
        {
            if (ModelState.IsValid)
            {
                if (TrGroup == 1)
                {
                    transaction.TrGroup = 1; 
                }
                else if (TrGroup == 2)
                {
                    transaction.TrGroup = 2;
                }

                if (TrGroup2 == 1)
                {
                    transaction.TrGroup2 = 1;
                }
                else if (TrGroup2 == 2)
                {
                    transaction.TrGroup2 = 2;
                }
                else if (TrGroup2 == 3)
                {
                    transaction.TrGroup2 = 3;
                }
                else if (TrGroup2 == 4)
                {
                    transaction.TrGroup2 = 4;
                }

                if (transaction.Name != null && transaction.TrGroup != null && transaction.TrGroup2 != null && transaction.Level != null)
                {
                    db.Transactions.Add(transaction);
                    db.SaveChanges();
                    return RedirectToAction("Index", "AdminTransaction");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập vào ô có dấu * !");
                }
            }

            return View(transaction);
        }

        public ActionResult Edit(int id = 0)
        {
            Transaction transaction = db.Transactions.Find(id);
            if (transaction == null)
            {
                return HttpNotFound();
            }
            return View(transaction);
        }

        [HttpPost]
        public ActionResult Edit(Transaction transaction, int TrGroup, int TrGroup2)
        {
            if (ModelState.IsValid)
            {
                if (TrGroup == 1)
                {
                    transaction.TrGroup = 1;
                }
                else if (TrGroup == 2)
                {
                    transaction.TrGroup = 2;
                }

                if (TrGroup2 == 1)
                {
                    transaction.TrGroup2 = 1;
                }
                else if (TrGroup2 == 2)
                {
                    transaction.TrGroup2 = 2;
                }
                else if (TrGroup2 == 3)
                {
                    transaction.TrGroup2 = 3;
                }
                else if (TrGroup2 == 4)
                {
                    transaction.TrGroup2 = 4;
                }

                db.Entry(transaction).State = EntityState.Modified;
                if (transaction.Name != null && transaction.TrGroup != null && transaction.TrGroup2 != null && transaction.Level != null)
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập vào ô có dấu * !");
                }
            }
            return View(transaction);
        }

        public ActionResult Delete(int id = 0)
        {
            Transaction transaction = db.Transactions.Find(id);
            if (transaction == null)
            {
                return HttpNotFound();
            }
            return View(transaction);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var transaction = db.Transactions.Find(id);
            db.Transactions.Remove(transaction);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
